import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Tags } from "./entities/tags.entity";
import { User } from "./entities/user.entity";
import { Inject } from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import type { Request } from "express";
import argon2 from "argon2";
import { MongooseRepository } from "./repositories/mongoose.repository";
import { PrismaRepository } from "./repositories/prisma.repository";
import { TypeOrmRepository } from "./repositories/typeorm.repository";
import { UserRepositoryAbstract } from "./user-abstract.repository";
import { UserAdapter } from "./user.interface";

export class UserRepository extends UserRepositoryAbstract implements UserAdapter {
    constructor(
        // @InjectRepository(User, 'mysql1') private readonly user: Repository<User>,
        // @InjectRepository(Tags, 'mysql1') private readonly tag: Repository<Tags>,
        // @InjectRepository(User, 'mysql2') private readonly user2: Repository<User>,
        // @InjectRepository(Tags, 'mysql2') private readonly tag2: Repository<Tags>,
        @Inject(REQUEST) private readonly request: Request,
        private readonly mongooseRepository: MongooseRepository,
        private readonly prismaRepository: PrismaRepository,
        private readonly typeormRepository: TypeOrmRepository,
    ) {
        super();
    }

    findAll(page?: number, limit?: number): Promise<any[]> {
        const client = this.getRepository();
        return client.findAll(page, limit);
    }

    findOne(username: string): Promise<any> {
        const client = this.getRepository();
        return client.findOne(username);
    }

    find(username?: string): Promise<any[]> {
        const client = this.getRepository();
        return client.find(username);
    }
    async create(userObj: any): Promise<any> {
        const client = this.getRepository();
        // 对密码进行hash加密
        const { password } = userObj;
        const hashPassword = await argon2.hash(password);
        userObj.password = hashPassword;
        return client.create(userObj);
    }
    update(userObj: any): Promise<any[]> {
        const client = this.getRepository();
        return client.update(userObj);
    }
    delete(id: string): Promise<any[]> {
        const client = this.getRepository();
        return client.delete(id);
    }


    getRepository(): any {
        const { query, headers } = this.request
        const { db } = query
        const tenantId = headers['x-tenant-id']

        if (tenantId?.includes('mongoose')) {
            return this.mongooseRepository
        } else if (tenantId?.includes('typeorm')) {
            return this.typeormRepository
        } else if (tenantId?.includes('prisma')) {
            return this.prismaRepository
        }

        /* if (db || tenantId === 'mysql2') {
            return {
                user: this.user2,
                tag: this.tag2
            }
        }
        return {
            user: this.user,
            tag: this.tag
        } */
    }
}